package com.ititer.web.controller;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;

import com.ititer.annotation.LoginRequired;
import com.ititer.bean.AccountBean;
import com.ititer.bean.CompetenceBean;
import com.ititer.enums.PublicKeys;
import com.ititer.model.Account;
import com.ititer.web.abs.AbstractController;
import com.ititer.web.module.Modules;

@Resource
@Path("/my")
public class My extends AbstractController {

	/**
	 * Enter user himself's home
	 */
	@Path("home")
	@Get
	@LoginRequired
	public void home() {
		setPageTitle("Welcome to home ~");
		Account account = getSession();
		if (StringUtils.isBlank(account.getUri())) {
			controllerResult.forwardTo(My.class).uriForm();
		} else {
			//TODO: load user saved models 
			//if is the first time login should display welcome page.
			int loginCount = accountSvc.getLoginCount(account.getId());
			//for test we use loginCount >=1
			//TODO: change to loginCount==1 after finish test
			if (loginCount >= 1) {
				addData(PublicKeys.AvaliableModules.getValue(), Modules.welcome);
			}
		}
	}

	/**
	 * Enter the user info init page for the first time login.
	 */
	@Path("uri")
	@Get
	@LoginRequired
	public void uriForm() {
		Account account = getSession();
		if (StringUtils.isNotBlank(account.getUri())) {
			controllerResult.redirectTo(My.class).home();
		}
		setPageTitle("Welcome " + account.getEmail() + ", please init your uri ~");
	}

	/**
	 * Save the user info for the first time.
	 */
	@Path("uri")
	@Post
	@LoginRequired
	public void postUri(String uri) {
		if (StringUtils.isBlank(uri)) {
			setErrorMessage("uri.is.required");
			controllerResult.forwardTo(My.class).uriForm();
		}
		boolean domainExist = accountSvc.isUriExist(uri);
		if (domainExist) {
			setErrorMessage("uri.is.exist", uri);
			controllerResult.forwardTo(My.class).uriForm();
		} else {
			Account account = getSession();
			account.setUri(uri);
			saveSession(account);
			AccountBean accountBean = new AccountBean();
			accountBean.bindFrom(account);
			boolean updated = accountSvc.updateUri(accountBean);
			if (updated) {
				controllerResult.redirectTo(Domain.class).domainForm();
			} else {
				toError();
			}
		}
	}

	@Get
	@Path("competence")
	@LoginRequired
	public void newCompetence() {
	}

	@Get
	@Path("competences")
	@LoginRequired
	public void listCompetence() {
		Account currentUser = getSession();
		List<CompetenceBean> competences = accountSvc.listCompetence(currentUser.getId());
		putData("competences", competences);
	}

	@Post
	@Path("competence")
	@LoginRequired
	public void saveCompetence(CompetenceBean competence) {
		validate(competence).onErrorForwardTo(My.class).newCompetence();
		Account account = getSession();
		competence.setAccountId(account.getId());
		competence.setLastUpdate(new Date());

		boolean result = accountSvc.saveCompetence(competence);
		competence.setSaved(result);

		outputJson("competence", competence);
	}
}
